package br.ufc.gui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import br.ufc.creator.Application;

/**
 * Painel que abriga o menu principal.
 */
@SuppressWarnings("serial")
public class MainPanel extends JPanel implements ActionListener {

	private final Font fieldTitleFont = new Font("SansSerif", Font.BOLD, 15);

	String[] hypervisors = { "KVM", "Xen" };
	String[] ubuntuVersions = { "10.04 - Lucid", "10.10 - Maverick" };
	String[] debianVersions = { "5.5 - Lenny", "6.0 - Squeeze" };

	// Mirror de onde serão baixados os pacotes
	JTextField mirrorField = new JTextField(20);

	// Hypervisor
	JLabel hypervisorLabel = new JLabel("Hypervisor");
	JComboBox hypervisorList = new JComboBox(hypervisors);

	// Distribuição GNU/Linux
	JLabel distroLabel = new JLabel("Distro");
	JRadioButton debianButton = new JRadioButton("Debian");
	JRadioButton ubuntuButton = new JRadioButton("Ubuntu");
	ButtonGroup distrosGroup = new ButtonGroup();

	// Versão
	JLabel versionLabel = new JLabel("Version");
	JComboBox versionList = new JComboBox();

	// Flavour "server" ou "virtual"
	JLabel flavourLabel = new JLabel("Kernel Flavour");
	JRadioButton virtualButton = new JRadioButton("Virtual");
	JRadioButton serverButton = new JRadioButton("Server");
	JRadioButton genericButton = new JRadioButton("Generic");
	ButtonGroup flavoursGroup = new ButtonGroup();

	// Arquitetura
	JLabel archLabel = new JLabel("Architecture");
	JRadioButton i386Button = new JRadioButton("i386");
	JRadioButton amd64Button = new JRadioButton("amd64");
	ButtonGroup archGroup = new ButtonGroup();

	// Informações de usuário
	JLabel nameLabel = new JLabel("Name");
	JTextField nameField = new JTextField(20);
	JLabel userLabel = new JLabel("User");
	JTextField userField = new JTextField(10);
	JLabel passwordLabel = new JLabel("Password");
	JPasswordField passwordField = new JPasswordField(10);

	/*JLabel hostnameLabel = new JLabel("Hostname");
	JTextField hostnameField = new JTextField(10);

	JTextArea instruction = new JTextArea(1, 100);*/

	JButton createButton = new JButton("Create");

	public MainPanel(LayoutManager layout) {
		super(layout);

		configureButtons();
		if (layout.getClass() == GridBagLayout.class) {
			GridBagConstraints gbc = new GridBagConstraints();

			gbc.fill = GridBagConstraints.HORIZONTAL;

			/*
			 * Define os espaços entre os componentes e as linhas do
			 * GridBagLayout. (top, left, bottom, right).
			 */
			gbc.insets = new Insets(2, 15, 2, 15);

			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.gridwidth = 1;
			hypervisorLabel.setFont(fieldTitleFont);
			add(hypervisorLabel, gbc);
			gbc.gridx = 0;
			gbc.gridy = 1;
			add(hypervisorList, gbc);
			gbc.gridx = 1;
			gbc.gridy = 0;
			distroLabel.setFont(fieldTitleFont);
			add(distroLabel, gbc);
			gbc.gridx = 1;
			gbc.gridy = 1;
			add(debianButton, gbc);
			gbc.gridx = 1;
			gbc.gridy = 2;
			add(ubuntuButton, gbc);
			gbc.gridx = 2;
			gbc.gridy = 0;
			versionLabel.setFont(fieldTitleFont);
			add(versionLabel, gbc);
			gbc.gridx = 2;
			gbc.gridy = 1;
			add(versionList, gbc);
			gbc.gridx = 3;
			gbc.gridy = 0;
			flavourLabel.setFont(fieldTitleFont);
			add(flavourLabel, gbc);
			gbc.gridx = 3;
			gbc.gridy = 1;
			add(serverButton, gbc);
			gbc.gridx = 3;
			gbc.gridy = 2;
			add(virtualButton, gbc);
			gbc.gridx = 3;
			gbc.gridy = 3;
			add(genericButton, gbc);
			gbc.gridx = 4;
			gbc.gridy = 0;
			archLabel.setFont(fieldTitleFont);
			add(archLabel, gbc);
			gbc.gridx = 4;
			gbc.gridy = 1;
			add(i386Button, gbc);
			gbc.gridx = 4;
			gbc.gridy = 2;
			add(amd64Button, gbc);
			gbc.gridx = 4;
			gbc.gridy = 4;
			add(createButton,gbc);
			/*
			 * add(versionList, constraints); add(nameField, constraints);
			 * add(userField, constraints); add(passwordField, constraints);
			 */
			setSize(480, 320);
		} else {

		}
	}

	/**
	 * Configura os Buttons do painel.
	 */
	private void configureButtons() {

		// Define as ações dos botões.
		debianButton.setActionCommand("debian");
		ubuntuButton.setActionCommand("ubuntu");
		serverButton.setActionCommand("server");
		virtualButton.setActionCommand("virtual");
		genericButton.setActionCommand("generic");
		i386Button.setActionCommand("i386");
		amd64Button.setActionCommand("amd64");
		createButton.setActionCommand("create");

		// Define as opções inicialmente marcadas.
		virtualButton.setSelected(true);
		amd64Button.setSelected(true);

		// Agrupa os JRadioButtons para que só um de cada grupo possa ser
		// marcado.
		distrosGroup.add(debianButton);
		distrosGroup.add(ubuntuButton);
		flavoursGroup.add(serverButton);
		flavoursGroup.add(virtualButton);
		flavoursGroup.add(genericButton);
		archGroup.add(i386Button);
		archGroup.add(amd64Button);

		// Registra um listener para os JRadioButtons.
		// debianButton.addActionListener(this);
		// debianButton.addActionListener(this);
		debianButton.addActionListener(this);
		ubuntuButton.addActionListener(this);
		createButton.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if ("ubuntu".equals(event.getActionCommand())) {
			versionList.removeAllItems();
			for (String version : ubuntuVersions) {
				versionList.addItem(version);
			}
		} else if ("debian".equals(event.getActionCommand())) {
			versionList.removeAllItems();
			for (String version : debianVersions) {
				versionList.addItem(version);
			}
		} else if ("create".equals(event.getActionCommand())) {
			Application.callVMBuilder(getHypervisor(), getDistro(), getVersion(), getFlavour(), getArch());
		}
	}
	
	private String getArch() {
		if (i386Button.isSelected()) {
			return "i386";
		} else {
			return "amd64";
		}
	}

	private String getHypervisor() {
		Object hypervisor = hypervisorList.getSelectedItem();
		if (hypervisor.equals("KVM")) {
			return "kvm";
		} else {
			return "xen";
		}
	}
	
	private String getDistro() {
		if (ubuntuButton.isSelected()) {
			return "ubuntu";
		} else {
			return "debian";
		}
	}
	
	private String getVersion() {
		Object version = versionList.getSelectedItem();
		if (version.equals("10.04 - Lucid")) {
			return "lucid";
		}
		if (version.equals("10.10 - Maverick")) {
			return "maverick";
		}
		if (version.equals("5.5 - Lenny")) {
			return "lenny";
		} else {
			return "squeeze";
		}
	}
	
	private String getFlavour() {
		if (virtualButton.isSelected()) {
			return "virtual";
		}
		if (serverButton.isSelected()) {
			return "server";
		}
		else {
			return "generic";
		}
	}
}
